#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QTimer *timer = new QTimer(this);

    connect(ui->push_button, &QPushButton::clicked, this, [=]() {
        if (ui->push_button->text() == "Move") {
            ui->push_button->setText("Stop");
            timer->start(50);

        } else if (ui->push_button->text() == "Stop") {
            ui->push_button->setText("Move");
            timer->stop();
        }
    });

    connect(timer, &QTimer::timeout, this, [=]() {
        x += 5;
        y += 2;
        update();
    });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    // 实例化画家对象, this 是指定的画图对象(需要画图的对象)
    QPainter painter(this);

    // 画线
    painter.drawLine(QPoint(0,0), QPoint(200, 100));
    painter.drawLine(QPoint(200, 100), QPoint(400, 0));

    // 设置画笔颜色
    QPen pen(Qt::red);
    painter.setPen(pen);

    // 画椭圆
    painter.drawEllipse(QPoint(200, 100), 100, 50);
    // 画圆
    painter.drawEllipse(QPoint(200, 200), 30, 30);

    // 设置抗锯齿, 可以使画家画出的图片更加清晰, 代价降低效率
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawEllipse(QPoint(250, 250), 30, 30);


    // 设置画刷
    QBrush brust(Qt::cyan);
    painter.setBrush(brust);

    // 画矩形
    painter.drawRect(QRect(30 + x, 200 + y, 100, 60));

    // 画文字
    painter.drawText(QRect(300, 200, 100, 30), "Good Good Study,");
    painter.drawText(QRect(300, 230, 100, 30), "Day Day Up!");

    // 利用画家 画资源图片
    painter.drawPixmap(x + 100, 400, QPixmap("D:\\Wallparper\\Min_Qt.png"));
}
